package com.example.zhunongzonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhunongzonghe.beans.ZnFarmEvaluateBean;
import com.example.zhunongzonghe.dao.ZnFarmEvaluateMapper;
import com.example.zhunongzonghe.dao.ZnOrderDetailsMapper;
import com.example.zhunongzonghe.dao.ZnOrderMapper;
import com.example.zhunongzonghe.entity.*;
import com.example.zhunongzonghe.service.IZnOrderDetailsService;
import com.example.zhunongzonghe.service.IZnUsersService;
import com.example.zhunongzonghe.service.ZnFarmEvaluateService;
import com.example.zhunongzonghe.service.ZnFarmSpecificationService;
import com.example.zhunongzonghe.utils.BeanCopyUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author 蔡某人
* @description 针对表【zn_farm_evaluate】的数据库操作Service实现
* @createDate 2024-04-17 16:39:26
*/
@Service
public class ZnFarmEvaluateServiceImpl extends ServiceImpl<ZnFarmEvaluateMapper, ZnFarmEvaluate>
    implements ZnFarmEvaluateService{

    @Resource
    private ZnFarmEvaluateMapper znFarmEvaluateMapper;
    @Resource
    private ZnOrderDetailsMapper znOrderDetailsMapper;
    @Resource
    private IZnUsersService usersService;
    @Resource
    private IZnOrderDetailsService detailsService;
    @Resource
    private ZnFarmSpecificationService specificationService;
    @Resource
    private ZnOrderMapper znOrderMapper;

    @Override
    public void addFarmEvaluate(ZnFarmEvaluateBean znFarmEvaluateBean,String userName) {
        ZnFarmEvaluate znFarmEvaluate = BeanCopyUtils.copyBean(znFarmEvaluateBean,ZnFarmEvaluate.class);
        znFarmEvaluate.setUsername(userName);
        znFarmEvaluate.setCreateTime(LocalDateTime.now());
        znFarmEvaluate.setUpdateTime(LocalDateTime.now());
        znFarmEvaluateMapper.insert(znFarmEvaluate);
        ZnOrderDetails znOrderDetails = znOrderDetailsMapper.selectById(znFarmEvaluateBean.getDetailsId());
        znOrderDetails.setEvaStatus(1);
        znOrderDetails.setEvaluateId(znFarmEvaluate.getId());
        znOrderDetailsMapper.updateById(znOrderDetails);
        ZnOrder znOrder = znOrderMapper.selectById(znFarmEvaluateBean.getOrderId());
        znOrder.setEvaluateStatus(1);
        znOrderMapper.updateById(znOrder);
    }

    @Override
    public void editFarmEvaluate(ZnFarmEvaluateBean znFarmEvaluateBean, String userName) {
        ZnFarmEvaluate znFarmEvaluate = BeanCopyUtils.copyBean(znFarmEvaluateBean,ZnFarmEvaluate.class);
        znFarmEvaluate.setUsername(userName);
        znFarmEvaluate.setUpdateTime(LocalDateTime.now());
        znFarmEvaluateMapper.updateById(znFarmEvaluate);
    }

    @Override
    public ZnFarmEvaluateBean getEvaluateId(Integer evaluateId) {
        LambdaQueryWrapper<ZnFarmEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnFarmEvaluate::getId,evaluateId);
        ZnFarmEvaluate znFarmEvaluate = znFarmEvaluateMapper.selectOne(queryWrapper);
        if (znFarmEvaluate==null) return null;
        ZnFarmEvaluateBean bean = BeanCopyUtils.copyBean(znFarmEvaluate,ZnFarmEvaluateBean.class);
        return bean;
    }

    @Override
    public Page<ZnFarmEvaluateBean> selectEvaAll(Integer farmId,Integer createPage, Integer pageSize) {
        Page<ZnFarmEvaluate> page = new Page<>(createPage,pageSize);
        LambdaQueryWrapper<ZnFarmEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnFarmEvaluate::getFarmId,farmId);
        Page<ZnFarmEvaluate> farmEvaluatePage = znFarmEvaluateMapper.selectPage(page,queryWrapper);
        Long sum = znFarmEvaluateMapper.selectCount(queryWrapper);
        Page<ZnFarmEvaluateBean> beanPage = BeanCopyUtils.copyBeanPage(farmEvaluatePage,ZnFarmEvaluateBean.class);
        List<ZnFarmEvaluateBean> farmEvaluateList = beanPage.getRecords();
        int averageScore = (int) farmEvaluateList.stream()
                .mapToInt(ZnFarmEvaluateBean::getScore)
                .average()
                .orElse(0.0);
        beanPage.getRecords().forEach(evaluateBean -> {
            ZnUsers znUsers = usersService.getUserName(evaluateBean.getUsername());
            evaluateBean.setAvatar(znUsers.getAvatar());
            evaluateBean.setNickname(znUsers.getNickname());
            ZnOrderDetails znOrderDetails =
                    detailsService.selectFarmIdAndSpecId(evaluateBean.getDetailsId());
            evaluateBean.setCount(znOrderDetails.getCount());
            ZnFarmSpecification specification = specificationService.SelectSpecId(evaluateBean.getFarmSpecId());
            evaluateBean.setZnSpecTitle(specification.getZnSpecTitle());
            evaluateBean.setZnSpecUnit(specification.getZnSpecUnit());
            evaluateBean.setAverageMarks(averageScore);
            evaluateBean.setEvaluateSum(sum);
        });
        return beanPage;
    }

    @Override
    public Page<ZnFarmEvaluateBean> selectFourAndFive(Integer farmId, Integer createPage, Integer pageSize) {
        Page<ZnFarmEvaluate> page = new Page<>(createPage,pageSize);
        LambdaQueryWrapper<ZnFarmEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnFarmEvaluate::getFarmId,farmId).eq(ZnFarmEvaluate::getScore, 5)
                .or(qw -> qw.eq(ZnFarmEvaluate::getFarmId,farmId).eq(ZnFarmEvaluate::getScore, 4));
        Page<ZnFarmEvaluate> farmEvaluatePage = znFarmEvaluateMapper.selectPage(page,queryWrapper);
        Long sum = EvaluateSum(farmId);
        int averageScore = averageScore(farmId);
        Page<ZnFarmEvaluateBean> beanPage = BeanCopyUtils.copyBeanPage(farmEvaluatePage,ZnFarmEvaluateBean.class);
        beanPage.getRecords().forEach(evaluateBean -> {
            ZnUsers znUsers = usersService.getUserName(evaluateBean.getUsername());
            evaluateBean.setAvatar(znUsers.getAvatar());
            evaluateBean.setNickname(znUsers.getNickname());
            ZnOrderDetails znOrderDetails =
                    detailsService.selectFarmIdAndSpecId(evaluateBean.getDetailsId());
            evaluateBean.setCount(znOrderDetails.getCount());
            ZnFarmSpecification specification = specificationService.SelectSpecId(evaluateBean.getFarmSpecId());
            evaluateBean.setZnSpecTitle(specification.getZnSpecTitle());
            evaluateBean.setZnSpecUnit(specification.getZnSpecUnit());
            evaluateBean.setAverageMarks(averageScore);
            evaluateBean.setEvaluateSum(sum);
        });
        return beanPage;
    }

    @Override
    public Page<ZnFarmEvaluateBean> selectThree(Integer farmId, Integer createPage, Integer pageSize) {
        Long sum = EvaluateSum(farmId);
        int averageScore = averageScore(farmId);
        Page<ZnFarmEvaluate> page = new Page<>(createPage,pageSize);
        LambdaQueryWrapper<ZnFarmEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnFarmEvaluate::getFarmId,farmId)
                    .eq(ZnFarmEvaluate::getScore, 3);
        Page<ZnFarmEvaluate> farmEvaluatePage = znFarmEvaluateMapper.selectPage(page,queryWrapper);
        Page<ZnFarmEvaluateBean> beanPage = BeanCopyUtils.copyBeanPage(farmEvaluatePage,ZnFarmEvaluateBean.class);
        beanPage.getRecords().forEach(evaluateBean -> {
            ZnUsers znUsers = usersService.getUserName(evaluateBean.getUsername());
            evaluateBean.setAvatar(znUsers.getAvatar());
            evaluateBean.setNickname(znUsers.getNickname());
            ZnOrderDetails znOrderDetails =
                    detailsService.selectFarmIdAndSpecId(evaluateBean.getDetailsId());
            evaluateBean.setCount(znOrderDetails.getCount());
            ZnFarmSpecification specification = specificationService.SelectSpecId(evaluateBean.getFarmSpecId());
            evaluateBean.setZnSpecTitle(specification.getZnSpecTitle());
            evaluateBean.setZnSpecUnit(specification.getZnSpecUnit());
            evaluateBean.setAverageMarks(averageScore);
            evaluateBean.setEvaluateSum(sum);
        });
        return beanPage;
    }

    @Override
    public Page<ZnFarmEvaluateBean> selectOneAndTow(Integer farmId, Integer createPage, Integer pageSize) {
        Long sum = EvaluateSum(farmId);
        int averageScore = averageScore(farmId);
        Page<ZnFarmEvaluate> page = new Page<>(createPage,pageSize);
        LambdaQueryWrapper<ZnFarmEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnFarmEvaluate::getFarmId,farmId).eq(ZnFarmEvaluate::getScore, 1)
                .or(qw -> qw.eq(ZnFarmEvaluate::getScore, 2).eq(ZnFarmEvaluate::getFarmId,farmId));
        Page<ZnFarmEvaluate> farmEvaluatePage = znFarmEvaluateMapper.selectPage(page,queryWrapper);
        Page<ZnFarmEvaluateBean> beanPage = BeanCopyUtils.copyBeanPage(farmEvaluatePage,ZnFarmEvaluateBean.class);
        beanPage.getRecords().forEach(evaluateBean -> {
            ZnUsers znUsers = usersService.getUserName(evaluateBean.getUsername());
            evaluateBean.setAvatar(znUsers.getAvatar());
            evaluateBean.setNickname(znUsers.getNickname());
            ZnOrderDetails znOrderDetails =
                    detailsService.selectFarmIdAndSpecId(evaluateBean.getDetailsId());
            evaluateBean.setCount(znOrderDetails.getCount());
            ZnFarmSpecification specification = specificationService.SelectSpecId(evaluateBean.getFarmSpecId());
            evaluateBean.setZnSpecTitle(specification.getZnSpecTitle());
            evaluateBean.setZnSpecUnit(specification.getZnSpecUnit());
            evaluateBean.setAverageMarks(averageScore);
            evaluateBean.setEvaluateSum(sum);
        });
        return beanPage;
    }

    @Override
    public Page<ZnFarmEvaluateBean> managementAll(Integer currentPage, Integer pageSize, Object searchInfo) {
        Page<ZnFarmEvaluateBean> page = new Page<>(currentPage,pageSize);
        MPJLambdaWrapper<ZnFarmEvaluate> mpjLambdaWrapper = new MPJLambdaWrapper<ZnFarmEvaluate>()
                .selectAll(ZnFarmEvaluate.class)
                .select(ZnUsers::getNickname)
                .select(ZnFarmproduce::getFarmName)
                .like(ZnFarmEvaluate::getContent,searchInfo)
                .or().like(ZnUsers::getNickname,searchInfo)
                .or().like(ZnFarmproduce::getFarmName,searchInfo)
                .innerJoin(ZnUsers.class,ZnUsers::getUserName,ZnFarmEvaluate::getUsername)
                .innerJoin(ZnFarmproduce.class,ZnFarmproduce::getFarmId,ZnFarmEvaluate::getFarmId);
        Page<ZnFarmEvaluateBean> beanPage = znFarmEvaluateMapper.selectJoinPage(page,ZnFarmEvaluateBean.class,mpjLambdaWrapper);
        return beanPage;
    }

    private int averageScore(Integer farmId){
        LambdaQueryWrapper<ZnFarmEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnFarmEvaluate::getFarmId,farmId);
        List<ZnFarmEvaluate> evaluateList = znFarmEvaluateMapper.selectList(queryWrapper);
        int averageScore = (int) evaluateList.stream()
                .mapToInt(ZnFarmEvaluate::getScore)
                .average()
                .orElse(0.0);
        return averageScore;
    }
    private Long EvaluateSum(Integer farmId){
        LambdaQueryWrapper<ZnFarmEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnFarmEvaluate::getFarmId,farmId);
        Long sum = znFarmEvaluateMapper.selectCount(queryWrapper);
        return sum;
    }
}




